﻿using ClientCommonClass;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace RemotingClient
{
    public class Program
    {
        #region 变量

        private System.Timers.Timer _timerKillTaskmgr;  // 循环杀“任务管理器”
        private Form frm_Lock; // 锁屏界面

        #endregion

        #region 函数入口

        /// <summary>
        /// 入口地址
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                Program program = new Program();
                program._initialSystem(); // 系统初始化

                #region 注册
                ClientMonitorOperation.Instance.RegisterChannel(); // 注册 TCP 信道
                ClientMonitorOperation.Instance.RegisterSendMessageEvent(
                    program.RemotingObject_SendMessageEvent); // 注册发送消息事件
                ClientMonitorOperation.Instance.RegisterShutDownPCEvent(
                    program.RemotingObject_ShutDownPCEvent); // 注册关机事件
                ClientMonitorOperation.Instance.RegisterLockPCEvent(
                    program.RemotingObject_LockPCEvent); // 注册锁屏事件
                ClientMonitorOperation.Instance.RegisterUnlockPCEvent(
                    program.RemotingObject_UnlockPCEvent); // 注册解屏事件
                #endregion
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
                Console.WriteLine(ex.Message);
#endif
            }

            Console.ReadKey(true);
        }

        #endregion

        #region 事件【客户端具体实现方法】

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg">内容</param>
        private void RemotingObject_SendMessageEvent(string msg)
        {
            new Thread(() =>
            {
                if (!string.IsNullOrWhiteSpace(msg))
                    MessageBox.Show(msg, "温馨提示",
                         MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                         MessageBoxOptions.ServiceNotification); // ServiceNotification: 可在 Win+L 时弹出窗口；DefaultDesktopOnly: 只能在登录系统后弹出窗口
            }).Start();
        }

        /// <summary>
        /// 关机
        /// </summary>
        private void RemotingObject_ShutDownPCEvent()
        {
            try
            {
                new Thread(() =>
                {
                    ClientUtilities.ShutDownPC();
                }).Start();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
                Console.WriteLine(ex.Message);
#endif

            }
        }

        /// <summary>
        /// 锁屏
        /// </summary>
        private void RemotingObject_LockPCEvent()
        {
            try
            {
                ClientUtilities.LockPC(); // 锁定鼠标及键盘
                _timerKillTaskmgr.Start(); // 检查用户是否按下 Ctrl + Alt + Del 打开任务管理器，杀死！

                new Thread(() =>
                {
                    if (frm_Lock != null && !frm_Lock.Visible)
                        frm_Lock.ShowDialog();
                }).Start();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
                Console.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 解屏
        /// </summary>
        private void RemotingObject_UnlockPCEvent()
        {
            try
            {
                ClientUtilities.UnlockPC(); // 解除鼠标及键盘的锁定
                _timerKillTaskmgr.Stop();

                new Thread(() =>
                {
                    if (frm_Lock != null && frm_Lock.Visible)
                        frm_Lock.Close();
                }).Start();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
                Console.WriteLine(ex.Message);
#endif
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 系统初始化
        /// </summary>
        private void _initialSystem()
        {
            #region Kill Taskmgr Timer
            Process[] process;
            _timerKillTaskmgr = new System.Timers.Timer();
            _timerKillTaskmgr.Interval = 1000; // 以毫秒为单位
            _timerKillTaskmgr.Elapsed += (s, arg) =>
            {
                try
                {
                    ClientUtilities.LockPC(); // 锁屏
                    process = Process.GetProcesses();
                    foreach (Process singleProcess in process)
                    {
                        try
                        {
                            if (singleProcess.ProcessName.ToLower().Trim() == "taskmgr") // 杀“任务管理器”进程
                            {
                                singleProcess.Kill();
                                break;
                            }
                        }
                        catch (System.Exception ex)
                        {
#if DEBUG
                            Debug.WriteLine(ex.Message);
                            Console.WriteLine(ex.Message);
#endif
                        }
                    }
                }
                catch (System.Exception ex)
                {
#if DEBUG
                    Debug.WriteLine(ex.Message);
                    Console.WriteLine(ex.Message);
#endif
                }
            };
            #endregion

            frm_Lock = new Form() // 新建锁屏界面
            {
                ShowInTaskbar = false,
                TopLevel = true,
                TopMost = true,
                BackColor = Color.Black,
                BackgroundImage = Image.FromFile(@"Resources\LockKey.png"),
                BackgroundImageLayout = ImageLayout.Center,
                FormBorderStyle = FormBorderStyle.None,
                WindowState = FormWindowState.Maximized,
            };
        }

        #endregion
    }
}
